package com.wenhuitec.android.tang.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.wenhuitec.android.tang.R;

public class LoginActivity extends AppCompatActivity {

    private Context mContext;
    private EditText login_username;
    private EditText login_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        SharedPreferences sharedPreferences =
                getSharedPreferences("systemI", Context.MODE_PRIVATE); //私有数据
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        mContext = this;
        login_username = findViewById(R.id.login_username);
        login_password = findViewById(R.id.login_password);
        findViewById(R.id.btn_login).setOnClickListener(view -> {
            String user = login_username.getText().toString();
            String pass = login_password.getText().toString();
            if ("".equals(user) || "".equals(pass)) {
                Toast.makeText(mContext, "请输入信息", Toast.LENGTH_LONG).show();
                return;
            }

            RequestUtils.login(mContext, user, pass, new MyObserverRes<String>(mContext) {
                @Override
                public void onSuccess(String result) {
                    Toast.makeText(mContext, "登录成功", Toast.LENGTH_LONG).show();
                    editor.putString("user", user);
                    editor.commit();
                    startActivity(new Intent(mContext, BottomTabActivity.class));
                    finish();
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    Toast.makeText(mContext, errorMsg, Toast.LENGTH_LONG).show();
                    login_username.setText("");
                    login_password.setText("");
                }
            });
        });
    }
}
